Syllabus, Last Week and Book
R Language - Part 1 and Part 2
R Language - Part 3
Workshop - MidTerm Project
Next Week (not next, the other one)
Extended Syllabus PDF
PDF - (Pg. 127-133 and 150-155)
Read
Write
Plot
*a <- 2.3
(6*a+42)/(3^(4.2-3.62))## [1] 29.50556
isTRUE((6*a+42)/(3^(4.2-3.62))==29.50556)## [1] FALSE
Problem: Compute double, triple or higher order integrals
Problem: Compute double, triple or higher order integrals
Problem: Compute double, triple or higher order integrals
# install.packages("cubature")
library(cubature)
cube_f <- function(x) 1
adaptIntegrate(cube_f,lowerLimit = c(0,0,0),upperLimit = c(4,4,4))## $integral
## [1] 64
##
## $error
## [1] 7.105427e-15
##
## $functionEvaluations
## [1] 33
##
## $returnCode
## [1] 0
# Any number with (or without) a decimal point.
a <- 3# Sub-class of the numeric class. The suffix L tells R to store.
a <- 3L# TRUE or FALSE - Logical Operators. < , > , == , >= , <= , != ...
a <- 3<2# Data type consists of letters or words. String. with quotes: " … "
a <- "3"is.XXX() and class()
name1 <- emir
name1 <- "emir"
name2 <- name1
name3 <- "name1"
number1 <- 32
number2 <- "32"
number3 <- 1:10
number4 <- seq(1,10)
var1 <- TRUE
var2 <- "TRUE"
answer1 <- is.logical(var1)
answer2 <- var1 + answer1 / 3
surname1 <- "toker"
print(name1)
print(surname1)
print(name1,surname1)
is.XXX() and class()
name <- "emir"
surname <- "toker"
print(c(name,surname)) # c means “combine”
Vector : The simplest data structure in R
name <- "emir"
surname <- "toker"
name_surname <- c(name,surname)
length(name_surname)
print(c("21","21"))
print(c("21",21))
print(c(21,21))
spring_months <- c("March", "April","May","June")
spring_months
length(spring_months)
dim(spring_months)
spring_months[1]
spring_months[3:4]
str(spring_months) # Structure
substr(spring_months, start = 1, stop = 3) # Substrings
strsplit(spring_months,"")
gsub("a", "A", spring_months) # Matching and Replacement
?str , ?substr , ?strsplit , ?gsub
Vectors indexed using two indices instead of one.
[ row, col ]
a <- c(1:3)
# str(a) and dim(a) and length(a)
b <- matrix(1:3, nrow = 1, ncol = 3)
# str(b) and dim(b) and length(b)
a <- c(1:3)
b <- matrix(1:3, nrow = 1, ncol = 3)## [1] 1 2 3
## [,1] [,2] [,3]
## [1,] 1 2 3
c <- matrix(1:9, nrow = 3, ncol = 3)## [,1] [,2] [,3]
## [1,] 1 4 7
## [2,] 2 5 8
## [3,] 3 6 9
d <- matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE)## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
my_mat <- matrix(runif(n=20, min=0, max=100), nrow = 4, ncol = 5)## [,1] [,2] [,3] [,4] [,5]
## [1,] 47.20439 12.63902 95.299868 56.496099 69.478405
## [2,] 38.58236 54.94485 24.343080 1.429381 67.859400
## [3,] 64.92683 86.18240 6.103165 85.902938 1.749736
## [4,] 96.42536 39.27326 84.330873 29.567810 95.258230
add <- matrix(seq(from=10, to=60, by=10), nrow = 2, ncol = 3)## [,1] [,2] [,3]
## [1,] 10 30 50
## [2,] 20 40 60
my_mat[2:3,2:4] <- add## [,1] [,2] [,3] [,4] [,5]
## [1,] 47.20439 12.63902 95.29987 56.49610 69.478405
## [2,] 38.58236 10.00000 30.00000 50.00000 67.859400
## [3,] 64.92683 20.00000 40.00000 60.00000 1.749736
## [4,] 96.42536 39.27326 84.33087 29.56781 95.258230
str(arr)## int [1:4, 1:3, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
# dim(arr)
# length(arr)x <- 1:24## [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24
arr <- array(x, dim = c(4,3,2)) #raw,col,level## , , 1
##
## [,1] [,2] [,3]
## [1,] 1 5 9
## [2,] 2 6 10
## [3,] 3 7 11
## [4,] 4 8 12
##
## , , 2
##
## [,1] [,2] [,3]
## [1,] 13 17 21
## [2,] 14 18 22
## [3,] 15 19 23
## [4,] 16 20 24
[ row, col, level ]
[ row, col, level ]
arr <- array(data=10:30,dim=c(2,5,2))## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 10 12 14 16 18
## [2,] 11 13 15 17 19
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 20 22 24 26 28
## [2,] 21 23 25 27 29
arr[2,2:4,1:2]## [,1] [,2]
## [1,] 13 23
## [2,] 15 25
## [3,] 17 27
arr[1,1:5,2]## [1] 20 22 24 26 28
array <- array(data=seq(2,144,2),dim=c(3,6,4))## , , 1
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 2 8 14 20 26 32
## [2,] 4 10 16 22 28 34
## [3,] 6 12 18 24 30 36
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 38 44 50 56 62 68
## [2,] 40 46 52 58 64 70
## [3,] 42 48 54 60 66 72
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 74 80 86 92 98 104
## [2,] 76 82 88 94 100 106
## [3,] 78 84 90 96 102 108
##
## , , 4
##
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 110 116 122 128 134 140
## [2,] 112 118 124 130 136 142
## [3,] 114 120 126 132 138 144
array[1,1,1:4]
array[1,1,]
array[1,1,4:1]
array[1,1,c(1:4!=2)]
array[1,1,2]
array[1,1,which(x==2)]
array[1,1,which(x<=2)]
array[1,1,2:4]
array[1,1,-1]
array[1,1,c(-1,-2)]
array[1,2:5,2]
array[1,c(2,3,4,5),2]
array[1,c(2,5),2]
array[1,c(2,5),2:3]
array[,c(2,5),2:3]
[ row, col, level ]
Factors are a special variable type for storing categorical variables.
They sometimes behave like strings, and sometimes like integers.
gender = c(male", "female", "male", "male", "female")
gender
class(gender)
str(gender)
gender[2]
gender_factor <- factor(c("male", "female", "male", "male", "female"))
gender_factor
class(gender_factor)
str(gender_factor)
gender_factor[2]
person=c("Peter","Lois","Meg","Chris","Stewie")
age=c(42,40,17,14,1)
sex=factor(c("M","F","F","M","M"))
married=c(TRUE,TRUE,FALSE,FALSE,FALSE)
den <- matrix(c(person,married),5,2)
den <- matrix(c(age,married),5,2)
den <- matrix(c(person,age),5,2)
den <- matrix(c(person,sex),5,2)
no need to Combine
df <- data.frame(person,married)
df
class(df)
dim(df)
length(df)
str(df)
df$
df$person
as.character(df$person)
df <- data.frame(person,age,sex,married,stringsAsFactors=FALSE)
df
str(df)
person="Brian"
age=7
sex=factor("M")
married=FALSE
new_record_row <- data.frame(person,age,sex,married)
new_df <- rbind(df,newrecord) # Combine R Objects by Rows
surname=c("Yilmaz","Zeki","Sahin","Caliskan","Uslu","Guzel")
new_record_col <- data.frame(surname, stringsAsFactors=FALSE)
new_df2 <- cbind(new_df, new_record_col) # Combine R Objects by Columns
new_df2
new_df2[c(5,6),]
new_df2[c(5,6),] <- new_df2[c(6,5),]
new_df2[5]
a <- 9:14
a
a[2]
a[2,]
b <- matrix(a,2,3)
b
b[2]
b[2,]
new_df2
new_df2[1]
length(new_df2[1])
dim(new_df2[1])
new_df2[[1]]
length(new_df2[[1]])
dim(new_df2[[1]])
new_df2[[1]][2]
new_df2[[1]][2:5]
new_df2$person[2]
new_df2$person[2:5]
new_df2
new_df2[2:3,1:5]
new_df2[2:3,]
new_df2[2,1]
new_df2[2:2,1:1]
attributes(new_df2)
matrix <- matrix(data=1:4,nrow=2,ncol=2)
vector <- c(T,F,T,T)
var <- "hello"
data_frame <- new_df2
list <- list(matrix,vector,var,data_frame)
class(list)
str(list)
dim(list)
length(list)
Read
Write
Plot
library(help="datasets")
list.files("/Users/emirtoker/Desktop/Dersler/Memurluk/Software_Tools_for_Earth_&_Environmental_Science/Software_Tools_R_Github/Presentation")
file.choose()
read.table(file = "18397_Cekmekoy_Omerli_15dk.txt")
read.table(file = "18397_Cekmekoy_Omerli_15dk.txt",
header=TRUE, sep=";")
read.table(file = "18397_Cekmekoy_Omerli_15dk.txt",
header=TRUE, sep=";", na.strings="-9999")
mydata_txt <- read.table(file = "18397_Cekmekoy_Omerli_15dk.txt",
header=TRUE,
sep=";",
na.strings="-9999")
str(mydata_txt)
mydata_csv <- read.csv(file="18397_Cekmekoy_Omerli.csv",
header=TRUE,
na.strings="-9999")
str(mydata_csv)
url <- "https://web.itu.edu.tr/tokerem/18397_Cekmekoy_Omerli_15dk.txt"
urldata_txt <- read.table(url,
header=TRUE,
sep=";",
na.strings="-9999")
Write .TXT and .CSV
write.table(x=urldata_txt,file="somenewfile.txt")
write.table(x=urldata_txt,file="somenewfile.txt",
sep=";",na="-9999",quote=FALSE,row.names=FALSE)
new_df2
write.table(x=new_df2,file="dffile.txt",
sep=";",na="-9999",quote=FALSE,row.names=FALSE)
write.table(x=new_df2,file="dffile.csv",
sep=";",na="-9999",quote=FALSE,row.names=FALSE)
foo <- c(1.1,2,3.5,3.9,4.2)
bar <- c(2,2.2,-1.3,0,0.2)
plot(foo,bar)
plot(foo,bar)
plot(foo,bar,type="l")
plot(foo,bar,type="b",main="My lovely plot",xlab="x axis label", ylab="location y")
plot(foo,bar,type="b",main="My lovely plot",xlab="",ylab="",col="red")
x <- 1:20
y <- c(-1.49,3.37,2.59,-2.78,-3.94,-0.92,6.43,8.51,3.41,-8.23,
-12.01,-6.58,2.87,14.12,9.63,-4.58,-14.78,-11.67,1.17,15.62)
plot(x,y,type="n",main="")
abline(h=c(-5,5),col="red",lty=2,lwd=2)
segments(x0=c(5,15),y0=c(-5,-5),x1=c(5,15),y1=c(5,5),col="red",lty=3,
lwd=2)
points(x[y>=5],y[y>=5],pch=4,col="darkmagenta",cex=2)
points(x[y<=-5],y[y<=-5],pch=3,col="darkgreen",cex=2)
points(x[(x>=5&x<=15)&(y>-5&y<5)],y[(x>=5&x<=15)&(y>-5&y<5)],pch=19,
col="blue")
points(x[(x<5|x>15)&(y>-5&y<5)],y[(x<5|x>15)&(y>-5&y<5)])
lines(x,y,lty=4)
arrows(x0=8,y0=14,x1=11,y1=2.5)
text(x=8,y=15,labels="sweet spot")
legend("bottomleft",
legend=c("overall process","sweet","standard",
"too big","too small","sweet y range","sweet x range"),
pch=c(NA,19,1,4,3,NA,NA),lty=c(4,NA,NA,NA,NA,2,3),
col=c("black","blue","black","darkmagenta","darkgreen","red","red"),
lwd=c(1,NA,NA,NA,NA,2,2),pt.cex=c(NA,1,1,2,2,NA,NA))
mydata_txt <- read.table(file = "18397_Cekmekoy_Omerli_15dk.txt",
header=TRUE,
sep=";",
na.strings="-9999")
mydata_txt
plot(mydata_txt$temp, type="l" )